import { Login_Status, Sdk_Platform, WX_VER_CONFIG } from "./constants"   // 轮子枚举

import db from "./db"   // 数据表
import load from "./load" // 资源加载
import pool from "./pool" // 对象池
import local from "./local" // 数据存储
import user from "./user"   // 用户相关
import voice from "./voice" // 声音


window.he = {
    db: db,
    load: load,
    pool: pool,
    local: local,
    user: user,
    // big: big,
    // func: comFunc,
    voice: voice,
    // net: net,
    check_init() {
        if (!window.gg) {
            cc.director.loadScene("Login")
            return true
        }
        return false
    },
    // 初始化sdk
    sdkInit() {
        // 此部分再 common.js 部分填写全局配置
        switch (common.sdk_env) {
            // 测试环境
            case Sdk_Platform.F_4399:
                window.cloud = cc.cloud && cc.cloud.initialize()
                // let auth = cloud.auth();
                // auth.signInAnonymously().then(res => {
                //     he.db.init()
                // });
                break
            case Sdk_Platform.VIVO:
                he.ad = require('./vivoSDK/vivoAd');
                window.cloud = cc.cloud && cc.cloud.initialize()
                let auth = cloud.auth();
                auth.signInAnonymously().then(res => {
                    he.db.init()
                });
                he.user.updateManager()
                break
            case Sdk_Platform.OPPO:
                he.ad = require('./oppoSDK/oppoAd');
                he.user.updateManager()
                break
            case Sdk_Platform.WECHAT:
                wx.cloud.init({
                    env: common.env,
                    traceUser: true
                })
                window.cloud = wx.cloud
                he.ad = require('./wxSDK/wxAd');
                he.share = require('./wxSDK/wxShare');
                he.share.onShare()
                he.user.updateManager()
                wx.tmSDK.init({
                    hideRequestLog: false,
                    appVersion: common.version
                });
                // 初始化云函数
                break
            case Sdk_Platform.UC:
                he.ad = require('./ucSDK/ucAd');
                window.cloud = cc.cloud && cc.cloud.initialize()
                let auth2 = cloud.auth();
                auth2.signInAnonymously().then(res => {
                    he.db.init()
                });
                he.user.updateManager()
                break
            case Sdk_Platform.HUAWEI:
                he.ad = require('./hwSDK/hwAd');
                if (!common.isTest) {
                    window.cloud = cc.cloud && cc.cloud.initialize()
                    auth = cloud.auth();
                    auth.anonymousAuthProvider().signIn().then(res => {
                        he.user.updateManager()
                    });
                } else {
                    he.user.updateManager()
                }
                break
                break
            case Sdk_Platform.DEBUG:
                // 测试
                he.db.init()

                //云服务
                // window.cloud = cc.cloud && cc.cloud.initialize()
                // let auth3 = cloud.auth();
                // auth3.signInAnonymously().then(res => {
                //     he.db.init()
                // });
                // he.user.updateManager()
                break
        }
        if (he.ad) {
            he.ad.init()
        }
    },
    // 开关sdk
    switchInit() {
        switch (common.sdk_env) {
            case Sdk_Platform.WECHAT:
                let version_info = {}
                for (let key in WX_VER_CONFIG) {
                    version_info[key] = false
                }
                let user = he.local.getJson('user')
                if (user) {
                    wx.tmSDK.sendUserInfo({ openId: user.openid, gender: 1 })
                }
                wx.tmSDK.getAppJSONConfig("shieldArea").then((shieldArea) => {
                    wx.tmSDK.login().then(login_info => {
                        if (shieldArea.scene && shieldArea.city && shieldArea.province) {
                            if (shieldArea.scene.indexOf(login_info.from_scene) >= 0 ||
                                shieldArea.city.indexOf(login_info.login_province) >= 0 ||
                                shieldArea.province.indexOf(login_info.login_city) >= 0
                            ) {
                                he.local.setJson('ver_info', version_info)
                                gg.eventManager.emit("login", Login_Status.TO_MAIN)
                                return
                            }
                        }
                        wx.tmSDK.getJudgeConfig().then(res => {
                            for (let key in res) {
                                let ver_item_info = res[key]
                                if (ver_item_info.status) {
                                    version_info[ver_item_info.key] = true
                                } else {
                                    version_info[ver_item_info.key] = false
                                }
                            }
                            he.local.setJson('ver_info', version_info)
                            gg.eventManager.emit("login", Login_Status.TO_MAIN)
                        }).catch(error => {
                            he.local.setJson('ver_info', version_info)
                            gg.eventManager.emit("login", Login_Status.TO_MAIN)
                        })
                    })
                });
                break
            case Sdk_Platform.VIVO:
                gg.eventManager.emit("login", Login_Status.TO_MAIN)
                he.log("登陆")
                break
            case Sdk_Platform.HUAWEI:
                if (common.isTest) {
                    gg.eventManager.emit("login", Login_Status.TO_MAIN)
                } else {
                    cloud.callFunction({
                        name: 'getVer',
                        data: {
                            version: common.version,
                            platform: common.sdk_env
                        }
                    }).then(res => {
                        //临时使用测试
                        let info = {}
                        console.log(res.result)
                        if (res.result) {
                            for (let i = 0; i < res.result.length; i++) {
                                if (res.result[i].platform == Sdk_Platform.HUAWEI) {
                                    let name = res.result[i].key
                                    info[name] = { type: res.result[i].type, value: res.result[i].value }
                                }
                            }
                            he.local.setJson('ver_info', info)
                        }
                        gg.eventManager.emit("login", Login_Status.TO_MAIN)
                    }).catch(err => {
                        console.log(err)
                        he.error(err)
                    })
                }
                break
            case Sdk_Platform.DEBUG:
            default:
                gg.eventManager.emit("login", Login_Status.TO_MAIN)
                break
        }
    },
    log() {
        return
        var len = arguments.length;
        let func = console.log
        if (len === 1) {
            func(arguments[0]);
        }
        else if (len === 2) {
            func(arguments[0], arguments[1]);
        }
        else if (len === 3) {
            func(arguments[0], arguments[1], arguments[2]);
        }
        else if (len === 4) {
            func(arguments[0], arguments[1], arguments[2], arguments[3]);
        }
        else if (len === 5) {
            func(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4]);
        }
        else if (len === 6) {
            func(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]);
        }
    },
    error() {
        // return
        let func = console.error
        var len = arguments.length;
        if (len === 1) {
            func(arguments[0]);
        }
        else if (len === 2) {
            func(arguments[0], arguments[1]);
        }
        else if (len === 3) {
            func(arguments[0], arguments[1], arguments[2]);
        }
        else if (len === 4) {
            func(arguments[0], arguments[1], arguments[2], arguments[3]);
        }
        else if (len === 5) {
            func(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4]);
        }
        else if (len === 6) {
            func(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]);
        }
    },
    warn() {
        let func = console.warn
        var len = arguments.length;
        if (len === 1) {
            func(arguments[0]);
        }
        else if (len === 2) {
            func(arguments[0], arguments[1]);
        }
        else if (len === 3) {
            func(arguments[0], arguments[1], arguments[2]);
        }
        else if (len === 4) {
            func(arguments[0], arguments[1], arguments[2], arguments[3]);
        }
        else if (len === 5) {
            func(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4]);
        }
        else if (len === 6) {
            func(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]);
        }
    },
}